package cn.wolfcode.luowowo2.comment.service.impl;

import cn.wolfcode.luowowo2.cache.domain.StrategyComment;
import cn.wolfcode.luowowo2.cache.repository.IStrategyCommentRepository;
import cn.wolfcode.luowowo2.cache.service.IStrategyCommentService;
import cn.wolfcode.luowowo2.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo2.common.query.StrategyCommentQueryObject;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.Optional;

/**
 * Created by Albert on 2019/7/11.
 */
@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IStrategyCommentRepository dao;

    @Override
    public boolean save(StrategyComment comment) {
        try {
            dao.save(comment);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQueryObject qo) {
        Query query = new Query();
        Criteria criteria = null;
        if (qo.getDetailId()!=-1) {
            criteria = Criteria.where("detailId").is(qo.getDetailId());
        }else {
            criteria = Criteria.where("detailId").ne(qo.getDetailId()); //ne not equal
        }
        query.addCriteria(criteria);
        long total = mongoTemplate.count(query, StrategyComment.class);
        if (total==0) {
            return Page.empty();
        }
        //pageRequest用于分页
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize()); //page从0开始
        query.with(pageRequest);//把分页信息加入

        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");

        query.with(sort);

        List<StrategyComment> list = mongoTemplate.find(query, StrategyComment.class);

        return new PageImpl<StrategyComment>(list,pageRequest,total);
    }

    @Override
    public Boolean thumbUp(String toid, Long fromid) {
        Optional<StrategyComment> optional = dao.findById(toid);
        StrategyComment comment = optional.get();
        List<Long> list = comment.getThumbuplist();
        Boolean flag = false;
        if(list.contains(fromid)){
            comment.setThumbupnum(comment.getThumbupnum() - 1);
            list.remove(fromid);
        }else{
            comment.setThumbupnum(comment.getThumbupnum() + 1);
            list.add(fromid);
            flag = true;
        }
        save(comment);
        return flag;
    }



}
